<cfcomponent extends="assets.csac.com.Base">

	<cffunction name="authorizeRequest" access="private" returntype="boolean">
		<cfargument name="token" type="string" required="false" default="foo" />
		<cfscript>
			/*if (arguments.token eq "53BCFE6D-C5C0-4E21-B35E-C6EECC727CF9") {
				return true;
			} else if (cgi.HTTP_HOST eq "calgrants" or cgi.HTTP_HOST eq "www.calgrants.org") {
				return true;
			} else {
				return false;	
			}*/
			return true;
		</cfscript>
	</cffunction>

	<cffunction name="read" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="obj" type="string" required="yes">
		<cfargument name="token" type="string" required="no" default="foo" />
		<cfscript>
			if (authorizeRequest(token = arguments.token)) {
				var gateway = CreateObject("component", "assets.csac.com.gateway.#arguments.obj#Gateway");
				return SerializeJSON(queryToStruct(gateway.query(argumentCollection = arguments)));
			} else {
				return false;
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="write" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="obj" type="string" required="yes">
		<cfargument name="token" type="string" required="no" default="foo" />
		<cfscript>
			if (authorizeRequest(token = arguments.token)) {
				var gateway = CreateObject("component", "assets.csac.com.gateway.#arguments.obj#Gateway");
				return SerializeJSON(queryToStruct(gateway.save(argumentCollection = arguments)));
			} else {
				return false;	
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="checkStock" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="Product_ID" type="numeric" default="0" />
		<cfargument name="productIDs" type="string" default="" />
		<cfscript>
			var productGateway = new assets.csac.com.gateway.ProductGateway();
		</cfscript>
		<cfreturn SerializeJSON(productGateway.checkStock(argumentCollection = arguments), true) />
	</cffunction>
	
	<!--- 
	
	<cffunction name="responseObj" access="private" output="false" returntype="struct">
		<cfscript>
			var responseObj = {};
			responseObj.success = true;
			responseObj.message = "";
			responseObj.data = "";
			responseObj.recordcount = "";
			return responseObj;
		</cfscript>
	</cffunction>
	
	<cffunction name="findHighSchools" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="term" type="string" required="yes">
		<cfset var responseObj = responseObj() />
		<cfset var result = "" />
		<cfset var params = StructNew() />
		<cfset params.term = arguments.term />
		
		<cfif cgi.HTTP_HOST contains "calgrants" or authorizeRequest()>
			<cftry>
				<cfinvoke component="assets.csac.com.gateway.Gateway" method="call" returnvariable="result">
					<cfinvokeargument name="objType" value="procedure" />
					<cfinvokeargument name="obj" value="findHighSchools" />
					<cfinvokeargument name="term" value="#arguments.term#" />
				</cfinvoke>				
				<cfif IsQuery(result)>
					<cfset responseObj["data"] = queryToArray(result) />
				<cfelse>
					<cfset responseObj["data"] = "No matches" />
				</cfif>
				
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	<cffunction name="_checkStock" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="Product_ID" type="numeric" default="0" />
		<cfargument name="productIDs" type="string" default="" />
		<cfreturn SerializeJSON(checkStock(argumentCollection = arguments), true) />
	</cffunction>
	
	<cffunction name="getProductQuantityRules" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="productIDs" type="string" default="" />
		<cfscript>
			var base = new assets.csac.com.Base();
		</cfscript>
		<cfreturn SerializeJSON(base.getProductQuantityRules(argumentCollection = arguments), true) />
	</cffunction>
	
	<cffunction name="read" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="obj" type="string" required="yes">
		<cfargument name="token" type="string" required="no" default="53BCFE6D-C5C0-4E21-B35E-C6EECC727CF9" />
		<cfset var result = "" />
		<cftry>
			<cfif cgi.HTTP_HOST eq "calgrants" or authorizeRequest(arguments.token)>
				<cfinvoke component="assets.csac.com.gateway.#arguments.obj#Gateway" method="query" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#" />
				</cfinvoke>
			</cfif>
			<cfcatch type="any">
				<cfreturn SerializeJSON(cfcatch) />
			</cfcatch>
		</cftry>
		<cfreturn SerializeJSON(queryToStruct(result), true) />
	</cffunction>
	
	<cffunction name="write" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="obj" type="string" required="yes">
		<cfargument name="token" type="string" required="no" default="53BCFE6D-C5C0-4E21-B35E-C6EECC727CF9" />
		<cfset var result = "" />
		<cftry>
			<cfif cgi.HTTP_HOST eq "calgrants" or authorizeRequest(arguments.token)>
				<cfinvoke component="assets.csac.com.gateway.#arguments.obj#Gateway" method="save" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#" />
				</cfinvoke>
			</cfif>
			<cfcatch type="any">
				<cfreturn SerializeJSON(cfcatch) />
			</cfcatch>
		</cftry>
		<cfreturn SerializeJSON(queryToStruct(result), true) />
	</cffunction>
	
	<cffunction name="query" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="objType" type="string" required="yes">
		<cfset var responseObj = initResponseObj() />
		<cfset var result = "" />
		<cfset var base = new assets.csac.com.Base() />
		
		<cfif authorizeRequest()>
			<cftry>
				<cfinvoke component="assets.csac.com.gateway.#arguments.objType#Gateway" method="query" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#" />
				</cfinvoke>
				
				<cfset responseObj["data"] = base.queryToStruct(result) />
				<cfset responseObj["recordcount"] = result.RecordCount />
				
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
			
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>	
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	<cffunction name="create" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="objType" type="string" required="yes">
		<cfset var responseObj = initResponseObj() />
		<cfset var result = "" />
		
		<cfif authorizeRequest()>
		
			<cftry>
				<cfinvoke component="assets.csac.com.gateway.#arguments.objType#Gateway" method="save" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#">
				</cfinvoke>
				
				<cfset responseObj["data"] = result />
			
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
			
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>	
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	<cffunction name="read" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="objType" type="string" required="yes">
		<cfset var result = "" />
		<cfset var base = new assets.csac.com.Base() />
		<cftry>
			<cfif authorizeRequest()>
				<cfinvoke component="assets.csac.com.gateway.#arguments.objType#Gateway" method="query" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#" />
				</cfinvoke>
			</cfif>
			<cfcatch type="any">
				<cfreturn SerializeJSON(cfcatch) />
			</cfcatch>
		</cftry>
		<cfreturn SerializeJSON(base.queryToStruct(result), true) />
	</cffunction>
	
	<cffunction name="update" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="objType" type="string" required="yes">
		<cfset var result = "" />
		<cftry>
			<cfif authorizeRequest()>
				<cfinvoke component="assets.csac.com.gateway.#arguments.objType#Gateway" method="save" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#">
				</cfinvoke>
			</cfif>
			<cfif NOT IsInstanceOf(result, "assets.csac.com.bo.#arguments.objType#")>
				<cfset returnVal = result />
			<cfelse>
				<cfset returnVal = true />
			</cfif>
			<cfcatch type="any">
				<cfset returnVal = cfcatch />
			</cfcatch>
		</cftry>
		<cfreturn SerializeJSON(returnVal) />
	</cffunction>
	
	<cffunction name="destroy" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="objType" type="string" required="yes">
		<cfset var responseObj = initResponseObj() />
		<cfset var result = "" />
		
		<cfif authorizeRequest()>
			
			<cftry>
				<cfinvoke component="assets.csac.com.gateway.#arguments.objType#Gateway" method="destroy" returnvariable="result">
					<cfinvokeargument name="argumentCollection" value="#arguments#">
				</cfinvoke>
				
				<cfset responseObj["data"] = result />
				
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
			
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>	
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	<cffunction name="chart" access="remote" output="false" returntype="any" returnformat="json">
		<cfset var isAuthorizedRequest = authorizeRequest() />
		<cfset var responseObj = initResponseObj() />
		<cfset var qObjs = "" />
		
		<cfif isAuthorizedRequest>
			<cftry>
				<cfinvoke component="ChartService" method="chart" returnvariable="qObjs">
					<cfinvokeargument name="argumentCollection" value="#arguments#" />
				</cfinvoke>
				
				<cfset responseObj["data"] = QueryToStruct(qObjs) />
				<cfset responseObj["recordcount"] = qObjs.RecordCount />
				
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
			
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>	
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	<cffunction name="call" access="remote" output="false" returntype="any" returnformat="json">
		<cfargument name="service" type="string" required="yes">
		<cfset var responseObj = initResponseObj() />
		<cfset var result = "" />
		
		<cfif authorizeRequest()>
			<cftry>
				<cfif arguments.service eq "procedure">
					<cfinvoke component="StoredProcedureService" method="call" returnvariable="result">
						<cfinvokeargument name="argumentCollection" value="#arguments#" />
					</cfinvoke>
				<cfelseif arguments.service eq "function">
					<cfinvoke component="FunctionService" method="#arguments.fx#" returnvariable="result">
						<cfinvokeargument name="argumentCollection" value="#arguments#" />
					</cfinvoke>
				</cfif>
				
				<cfif IsQuery(result)>
					<cfset responseObj["data"] = QueryToStruct(result) />
				<cfelse>
					<cfset responseObj["data"] = result />
				</cfif>
				
				<cfcatch type="any">
					<cfset responseObj["success"] = false />
					<cfset responseObj["message"] = cfcatch />
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset responseObj["success"] = false />
			<cfset responseObj["message"] = "Invalid request" />
		</cfif>
		
		<cfreturn SerializeJSON(responseObj, true) />
	</cffunction>
	
	 --->
	
</cfcomponent>